<?php 
namespace app\index\controller;

use app\common\controller\Curls;
use app\common\controller\Marcketline;

class Marcket extends Base
{
	/**
	 * 获取市场交易行情
	 * @return unknown
	 */
	public function getLines()
	{
		$coin = $this->reqeust -> post('coin');
		//获取所有交易对
		$range = Curls::curl_post('https://api.huobi.pro/v1/common/symbols');
		$range = json_decode($range,true)['data'];
		//获取交易行情
		$res = Curls::curl('https://api.huobi.pro/market/tickers');
		$data = json_decode($res,true)['data'];
		foreach ($data as &$rows) {
			$rows['close'] = floatval($rows['close']);
			$rows['open'] = floatval($rows['open']);
			$num = (($rows['close'] - $rows['open']) / $rows['open']) * 100;
			$num = round($num,2);
			$rows['rate'] = $num;
		}
		//从大到小排序
		$last_ages = array_column($data,'close');
		array_multisort($last_ages ,SORT_DESC,$data);
		//初始化交易行情数组
		$range_list = [];
		//整合数据
		foreach ($data as &$val) {
			foreach ($range as $v) {
				if($val['symbol'] == $v['symbol']) {
					$val['base_currency'] = strtoupper($v['base-currency']);
					$val['quote_currency'] = strtoupper($v['quote-currency']);
				}
			}
			if(!empty($val['quote_currency']) && $val['quote_currency'] == $coin) {
				$range_list[] = $val;
			}
		}
		return jsonSuccess($range_list);
	}
	
	/**
	 * 获取交易对K线图数据
	 * @return unknown
	 */
	public function kLine()
	{
		$data = $this->reqeust->post('');
		$symbol = $data['symbol'];
		$period = $data['period'];
		$size = $data['size'];
		$url = "https://api.huobi.pro/market/history/kline?period={$period}&size={$size}&symbol={$symbol}";
		$res = Curls::curl($url);
		$res = json_decode($res,true);
		$res = $res['data'];
		//从大到小排序
		$last_ages = array_column($res,'id');
		array_multisort($last_ages ,SORT_ASC,$res);
		//初始化X轴
		$k_x = [];
		//初始化K线图名称
		$k_name = '';
		//初始化K线图数据
		$k_data = [];
		foreach ($res as &$row) {
			switch ($period) {
				case '1min':
					$row['time'] = date('H:i',$row['id']);
					break;
				case '15min':
					$row['time'] = date('H:i',$row['id']);
					break;
				case '30min':
					$row['time'] = date('H:i',$row['id']);
					break;
				case '60min':
					$row['time'] = date('H:i',$row['id']);
					break;
				case '1day':
					$row['time'] = date('Y/m/d',$row['id']);
					break;
			}
// 			$row['time'] = date('Y/m/d',$row['id']);
			$k_x[] = $row['time'];
			$k_name = $symbol.'价格K线图';
			$k_data[] = [$row['open'],$row['close'],$row['low'],$row['high']];
		}
		//重组K线图数据
		$result = [
				'categories' => $k_x,
				'series' => [
						[
								'name' => $k_name,
								'data' => $k_data
						]
				]
		];
		return jsonSuccess($result);
	}

	/**
	 * 交易深度数据
	 * @return unknown
	 */
	public function deepLine($return = false)
	{
		$data = $this->reqeust->post('');
		$symbol = $data['symbol'];  //交易对
		$depth = $data['depth'];   //返回深度的数量	
		$type = $data['type']; //深度的价格聚合度
		$deep = new Marcketline();
		$res = $deep ->deepLine($symbol, $depth, $type);
		return jsonSuccess($res);
	}
	
	
	/**
	 * 交易对交易记录
	 * @return unknown
	 */
	public function tradeRecord()
	{
		$data = $this->reqeust->post('');
		$symbol = $data['symbol'];  //交易对
		$size = $data['size'];  //查询数量
		$record = new Marcketline();
		$res = $record->tradeRecord($symbol, $size);
		return jsonSuccess($res);
	}
}